package com.ly.web.controller.king;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.heart.domain.KingBaseCostCenterEntity;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.service.IKingBaseCostCenterService;
import com.ly.heart.service.IKingOrgService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.heart.util.KingCommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api (value = "CostCenterRemote", tags = "推基础数据-成本中心")
@RestController
@RequestMapping ("/heart/CostCenterRemote")
@Slf4j
public class CostCenterRemote {

    @Resource
    private IKingBaseCostCenterService iKingBaseCostCenterService;
    @Resource
    private IKingOrgService iKingOrgService;

    @ApiOperation ("同步拉取金蝶成本中心")
    @RequestMapping ("pull")
    public AjaxResult queryList () {
        KingOrgEntity orgEntity = iKingOrgService.getOne ( new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getOrgId, CurrentUserUtil.getCompanyId () ).eq ( KingOrgEntity :: getIsDisabled, false ) );
        if ( orgEntity == null ) {
            throw new CustomException ( "请先至系统管理中配置金蝶账套！" );
        }
        IdentifyInfo identifyInfo = KingCommonUtil.getIdentifyInfo ( orgEntity );
        K3CloudApi client = new K3CloudApi ( identifyInfo );
        //请求参数，要求为json字符串
        String jsonData = "{\"FormId\":\"" + KingConstUtil.CB_COSTCENTER + "\",\"FieldKeys\":\"FNumber,FName,FDescription,FBUSINESSTYPE,FRELATIONTYPE,FRELATION.FNumber\",\"FilterString\":[],\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery ( jsonData );
            for ( int i = 0; i < lists.size (); i++ ) {
                List<Object> objects = lists.get ( i );
                KingBaseCostCenterEntity costCenterEntity = iKingBaseCostCenterService.getOne ( new QueryWrapper<KingBaseCostCenterEntity> ().eq ( "cost_code", objects.get ( 0 ) ) );
                if ( costCenterEntity == null ) {
                    costCenterEntity = new KingBaseCostCenterEntity ();
                    costCenterEntity.setCostCode ( String.valueOf ( objects.get ( 0 ) ) );
                    costCenterEntity.setTitle ( String.valueOf ( objects.get ( 1 ) ) );
                    costCenterEntity.setCostDesc ( String.valueOf ( objects.get ( 2 ) ) );
                    costCenterEntity.setDeptAttr ( String.valueOf ( objects.get ( 3 ) ) );//属性
                    costCenterEntity.setRelationType ( String.valueOf ( objects.get ( 4 ) ) );//关联对象类型
                    costCenterEntity.setRelation ( String.valueOf ( objects.get ( 5 ) ) );//关联对象
                    costCenterEntity.setIsSend ( KingConstUtil.KING_DATA_IS_SEND_YES );
                    iKingBaseCostCenterService.insertKingBaseCostCenterEntity ( costCenterEntity );
                }
                //根据code查询是否已存在该结算方式
                log.info ( "--->{}", objects );
            }
        } catch ( Exception e ) {
            throw new RuntimeException ( "同步成本中心失败" );
        }
        return AjaxResult.success ( "同步成本中心成功" );
    }


}
